package com.ctshk.rpc.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.cms.dto.AppVersionDTO;
import com.ctshk.rpc.cms.entity.AppVersion;
import com.ctshk.rpc.cms.mapper.AppVersionMapper;
import com.ctshk.rpc.cms.req.AppVersionAddReq;
import com.ctshk.rpc.cms.req.AppVersionQueryReq;
import com.ctshk.rpc.cms.req.AppVersionUpdateReq;
import com.ctshk.rpc.cms.req.AppVersionUpdateStatusReq;
import com.ctshk.rpc.cms.service.IAppVersionService;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.service.ISysUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * app版本 服务实现类
 * </p>
 *
 * @author LiZhiming
 * @since 2021-04-23
 */
@DubboService
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersion> implements IAppVersionService {

    @DubboReference
    private ISysUserService sysUserService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result insert(AppVersionAddReq req, TokenUser tu) {
        AppVersion av = EntityUtil.copy(req, AppVersion.class);
        LocalDateTime now = LocalDateTime.now();
        Long id = IdWorker.getId();
        av.setCreateId(tu.getId());
        av.setGmtCreate(now);
        av.setId(id);
        av.setIsDeleted(IsDeletedCode.NO.getCode());
        int insert = baseMapper.insert(av);
        if (insert > 0) {
            return Result.success(id);
        }
        return Result.failed(SystemError.CMS_23000);
    }

    @Override
    public Result<AppVersionDTO> queryById(Long id) {
        AppVersionDTO b = null;
        QueryWrapper<AppVersion> qw = new QueryWrapper<>();
        qw.lambda().eq(AppVersion::getIsDeleted, IsDeletedCode.NO.getCode())
                .eq(AppVersion::getId, id);
        AppVersion av = baseMapper.selectOne(qw);
        if (av != null) {
            b = EntityUtil.copy(av, AppVersionDTO.class);
        }
        return Result.success(b);
    }

    @Override
    public PageResponse<AppVersionDTO> queryList(AppVersionQueryReq req) {
        Page<AppVersion> page = new Page(req.getPageNo(), req.getPageSize());
        QueryWrapper<AppVersion> qw = new QueryWrapper();
        qw.lambda().eq(AppVersion::getIsDeleted, IsDeletedCode.NO.getCode())
                .eq(req.getAppTypeId() != null, AppVersion::getAppTypeId, req.getAppTypeId())
                .eq(req.getAppTypeName() != null, AppVersion::getAppTypeName, req.getAppTypeName())
                .eq(req.getStatus() != null, AppVersion::getStatus, req.getStatus());
        Page<AppVersion> avPage = baseMapper.selectPage(page, qw);
        if (CollectionUtils.isNotEmpty(avPage.getRecords())) {
            List<AppVersionDTO> avs = EntityUtil.copyList(avPage.getRecords(), AppVersionDTO.class);
            for (AppVersionDTO av : avs) {
                UserDTO userDTO = sysUserService.loadUserInfo(av.getCreateId(), 1);
                if (av != null) {
                    av.setUserName(userDTO.getCnSurname() + userDTO.getCnName());
                }
            }
            return new PageResponse<>(avs,
                    page.getCurrent() == 1, page.getTotal() <= (page.getCurrent() *
                    page.getSize()), page.getTotal(), page.getCurrent(), page.getSize());
        }
        return new PageResponse<>(null,
                page.getCurrent() == 1, page.getTotal() <= (page.getCurrent() *
                page.getSize()), page.getTotal(), page.getCurrent(), page.getSize());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result update(AppVersionUpdateReq req, TokenUser tu) {
        LocalDateTime now = LocalDateTime.now();
        AppVersion copy = EntityUtil.copy(req, AppVersion.class);
        copy.setModifiedId(tu.getId());
        copy.setGmtModified(now);
        int i = baseMapper.updateById(copy);
        if (i > 0) {
            return Result.success(req.getId());
        }
        return Result.failed(SystemError.CMS_23002);
    }

    @Override
    public Result updateByStatus(AppVersionUpdateStatusReq req) {
        int i = baseMapper.updateStatusById(req.getId(), req.getStatus());
        if (i > 0) {
            return Result.success(req.getId());
        }
        return Result.failed(SystemError.CMS_23002);
    }
}
